/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-21
 * Time: 22:49
 */
//https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/
public class Test {
    public static int findMin(int[] arr) {
        int size=arr.length;
        if(size==1){
            return arr[0];
        }
        if(arr[0]<arr[1]&&arr[0]<arr[size-1]){
            return arr[0];
        }
        if(arr[size-1]<arr[size-2]&&arr[size-1]<arr[0]){
            return arr[size-1];
        }
        int p1=1,p2=size-2;
        int p=(p1+p2)/2;
        while(p1<=p2){
            if(arr[p]<arr[p-1]&&arr[p]<arr[p+1]){
                return arr[p];
            }
            if(arr[p]<arr[p2]){
                p2=p;
            }
            else{
                p1=p+1;
            }
            p=(p1+p2)/2;
        }
        return arr[p];
    }
    public static void main(String[] args) {
        int[] arr={4,5,6,7,0,1,2};
        System.out.println(findMin(arr));
    }
}
